package com.tzinfo.tzt.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.tzinfo.data.TztData;
import com.tzinfo.tzt.R;
import com.tzinfo.tzt.activity.util.FragmentHomeListviewAdapter;
import com.tzinfo.tzt.util.HttpUtils;
import com.tzinfo.tzt.util.ResultMessage;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

public class UserFragment extends Fragment {
    private ListView attentionListView;
    private Context context;
    private FragmentHomeListviewAdapter fragmentHomeListviewAdapter;

    public UserFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        if (TztData.isUserLogin) {

            View view = inflater.inflate(R.layout.fragment_attentionlist, container, false);
            attentionListView=(ListView)view.findViewById(R.id.fragment_attentionList_listView);
            GetDataTask a = new GetDataTask();
            a.execute();
            return view;
        }
        else
            return inflater.inflate(R.layout.fragment_user, container, false);
    }

    // TODO: Rename method, update argument and hook method into UI event


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        context = activity;
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    public class GetDataTask extends AsyncTask<String, Void, ResultMessage> {


        @Override
        protected ResultMessage doInBackground(String... params) {
            List<NameValuePair> pairs = new ArrayList<NameValuePair>();
            ResultMessage result = null;
            pairs.add(new BasicNameValuePair("Sessionid", TztData.userInfo.getData().getSessionID()));
            //   pairs.add(new BasicNameValuePair("Title", ""));
            pairs.add(new BasicNameValuePair("page", "1"));
            pairs.add(new BasicNameValuePair("pagesize", String.valueOf(1000)));
            String GetDataUrl = "http://auto.tzinfo.net/mob/api/AttentionList.ashx";
            String car4sDataString = HttpUtils.doPostMethod(GetDataUrl, pairs);
            Gson gson = new Gson();
            result = gson.fromJson(car4sDataString, ResultMessage.class);
            return result;
        }

        @Override
        protected void onPostExecute(ResultMessage result) {
            super.onPostExecute(result);
            if (result.getSuccess() == true) {
                fragmentHomeListviewAdapter = new FragmentHomeListviewAdapter(context,result.getData()) ;
                attentionListView.setAdapter(fragmentHomeListviewAdapter);
            } else {
                Toast.makeText(context, result.getErrorMsg(), Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

}
